package cn.pfund.controller.shop;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.News;
import cn.pfund.entity.NewsType;
import cn.pfund.service.NewsService;
import cn.pfund.service.NewsTypeService;
import cn.pfund.util.ParamBean;

/**
 * 新闻管理
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("shopNewsController")
@RequestMapping("/shop/news")
public class NewsController extends BaseController {
    @Resource(name="newsService")
    private NewsService newsService;
    @Resource(name="newsTypeService")
    private NewsTypeService newsTypeService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
    	List<NewsType> newsTypes = newsTypeService.getAllNewsType();
    	NewsType nType = null;
    	if (paramBean.getNewsTypeId() != null && paramBean.getNewsTypeId().longValue() > 0) {
			for (NewsType newsType : newsTypes) {
				if (newsType.getId().longValue() == paramBean.getNewsTypeId().longValue()) {
					nType = newsType;
					break;
				}
			}
		} else {
			nType = newsTypes.get(0);
		}
    	request.setAttribute("newsTypes", newsTypes);
    	request.setAttribute("nType", nType);
		return "shop/news/list";
	}
    
	@RequestMapping(value = "/detail",method=RequestMethod.GET)
	public String detail(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		if (paramBean.getNewsId() == null || paramBean.getNewsId().longValue() < 1) {
			return ERROR_VIEW;
		}
		News news = newsService.getById(News.class, paramBean.getNewsId());
		if (news == null) {
			return ERROR_VIEW;
		}
		List<NewsType> newsTypes = newsTypeService.getAllNewsType();
		request.setAttribute("news", news);
		request.setAttribute("newsTypes", newsTypes);
		return "shop/news/detail";
	}
}